package cn.com.dycc.sys_manage.t_class.service;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.dycc.sys_manage.t_class.dao.ClassDao;
import cn.com.dycc.sys_manage.t_class.entity.ClassEntity;
import cn.com.dycc.sys_manage.t_class.param.ClassParam;
import cn.com.dycc.sys_manage.t_class.param.FindListClassParam;
import cn.com.dycc.sys_manage.usersysmanagement.dao.UserChildAccountDao;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.IntegerUtil;
import cn.com.dycc.util.Paging;

@Service("classService")
@Transactional
public class ClassServiceImpl implements ClassService{
	
	@Resource(name="classDao")
	private ClassDao classDao;
	
	@Resource(name="paging")
	private Paging paging;
	
	@Resource(name="userChildAccountDao")
	private UserChildAccountDao userChildAccountDao;
	
	//增加职务信息 HttpServletRequest request, 
	public String addClass(HttpServletRequest request, ClassParam classParam) throws NotFoundClassException {
		
		if(classParam.getClass_name()==null||classParam.getClass_name().trim().isEmpty()){
			throw new NotFoundClassException("请输入职务名称！");
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		//是否已存在同名的职务
		int countName = classDao.findCountClassByClassName(org_id, classParam.getClass_name());
		if(countName>=1){
			throw new NotFoundClassException("添加的职务名称已存在！");
		}
		
		if(classParam.getOrder_no()!=null&&!(IntegerUtil.isNumeric(classParam.getOrder_no().toString()))){
			throw new NotFoundClassException("排序类型为整型！");
		}
		
		
		String opt_user_id = request.getSession().getAttribute("id").toString();
//		String org_id = "1";
//		String opt_user_id = "1";
		if(opt_user_id==null){
			throw new NotFoundClassException("请登录！");
		}
		
		classParam.setOrg_id(org_id);
		classParam.setOpt_user_id(opt_user_id);
		
		int add = classDao.addClass(classParam);
		
		if(add == 0){
			throw new NotFoundClassException("增加职务失败！");
		}
		
		return "增加成功！";
	}
	
	//查询所有的职务信息 HttpServletRequest request,
	public List<ClassEntity> findClassAll(HttpServletRequest request,FindListClassParam findListClassParam) throws NotFoundClassException {
		
		Integer limit = findListClassParam.getLimit();
		Integer page = findListClassParam.getPage();
		
		if(!(IntegerUtil.isNumeric(page.toString()))){
			throw new NotFoundClassException("查询页码为整型！");
		}
		
		Integer start = null;
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
//		String org_id = "1";
		
		findListClassParam.setLimit(limit);
		findListClassParam.setStart(start);
		findListClassParam.setOrg_id(org_id);
		findListClassParam.setState("Y");
		
		return classDao.findClassAll(findListClassParam);
	}
	
	//更新职务信息  HttpServletRequest request, 
	public String updateClass(HttpServletRequest request,ClassParam classParam) throws NotFoundClassException {
		
		if(classParam.getId()==null||classParam.getId().trim().isEmpty()){
			throw new NotFoundClassException("请选择需要更新的职务！");
		}
		
		if(!(IntegerUtil.isNumeric(classParam.getId()))){
			throw new NotFoundClassException("职务的id为整型！");
		}
		
		if(classParam.getClass_name()==null||classParam.getClass_name().trim().isEmpty()){
			throw new NotFoundClassException("请输入职务名称！");
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
//		String org_id = "1";
		
		if(!(classParam.getClass_name().equals(classDao.findClassNameByid(classParam.getId(),org_id)))){
			//是否已存在同名的职务
			int countName = classDao.findCountClassByClassName(org_id, classParam.getClass_name());
			if(countName>=1){
				throw new NotFoundClassException("更新的职务名称已存在！");
			}
		}
		
		
		if("N".equals(classParam.getState())){
			int count = userChildAccountDao.findCountByClassNo(org_id,classParam.getId());
			if(count>=1){
				throw new NotFoundClassException("该职务下还有人员在使用！");
			}
		}
		
		if(classParam.getOrder_no()!=null&&!(IntegerUtil.isNumeric(classParam.getOrder_no().toString()))){
			throw new NotFoundClassException("排序类型为整型！");
		}
		
		String update_user_id = request.getSession().getAttribute("id").toString();	
//		String update_user_id = "1";
		if(update_user_id==null){
			throw new NotFoundClassException("请登录！");
		}
		String format = "yyyy-MM-dd HH:mm:ss";
		String update_date = DateUtil.getNowDate(format);
		
		classParam.setUpdate_date(update_date);
		classParam.setUpdate_user_id(update_user_id);
		classParam.setOrg_id(org_id);
		
		int countAdd = classDao.updateClass(classParam);
		if(countAdd==0){
			throw new NotFoundClassException("更新失败！");
		}
		
		return "更新成功！";
	}
	
	//删除职务信息
	public String delClass(HttpServletRequest request,String id) throws NotFoundClassException {
		
		if(id == null || id.trim().isEmpty()){
			throw new NotFoundClassException("请选择需要删除的职务！");
		}
		
		if(!(IntegerUtil.isNumeric(id))){
			throw new NotFoundClassException("职务的id为整型！");
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		
		int count = userChildAccountDao.findCountByClassNo(org_id,id);
		
		if(count>=1){
			throw new NotFoundClassException("该职务下还有人员在使用！");
		}
		
		int del = classDao.delClass(id,org_id);
		if(del==0){
			throw new NotFoundClassException("删除失败！");
		}
		
		return "删除成功！";
	}
	
	//查询所有的职务信息 HttpServletRequest request,统计
	public String findClassAllCount(HttpServletRequest request, FindListClassParam findListClassParam)
			throws NotFoundClassException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		findListClassParam.setOrg_id(org_id);
		findListClassParam.setState("Y");
		return classDao.findClassAllCount(findListClassParam);
	}

}
